package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.Anchor;
import com.originskyseed.platform.user.model.AnchorRelateAnnex;
import com.originskyseed.platform.user.model.AnchorRelateGroup;
import com.originskyseed.platform.user.service.IAnchorRelateAnnexService;
import com.originskyseed.platform.user.service.IAnchorRelateGroupService;
import com.originskyseed.platform.user.service.IAnchorService;
import com.originskyseed.platform.user.vo.request.AnchorRelateAnnexRequestVO;

/**
 * 主播关联附件信息表Controller类（OSI_ANCHOR_RELATE_ANNEX）
 *
 * @author
 */
@Controller
@RequestMapping("/anchorRelateAnnex")
@BaseLocaleResource(localeFileName = "i18n_user")
public class AnchorRelateAnnexController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AnchorRelateAnnexController.class);

	@DubboReference
	private IAnchorRelateAnnexService anchorRelateAnnexService;

	@DubboReference
	private IAnchorService anchorService;

	@DubboReference
	private IAnchorRelateGroupService anchorRelateGroupService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorRelateAnnexList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<AnchorRelateAnnex>> anchorRelateAnnexList(HttpServletRequest request) {
		BaseResponseBody<BasePage<AnchorRelateAnnex>> result = null;
		try {
			// 请求对象
			AnchorRelateAnnexRequestVO requestVO = this.getParameterBean(AnchorRelateAnnexRequestVO.class);

			// 分页查询
			BasePage<AnchorRelateAnnex> page = new BasePage<AnchorRelateAnnex>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.anchorRelateAnnexService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<AnchorRelateAnnex>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<AnchorRelateAnnex>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorRelateAnnexSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<AnchorRelateAnnex> anchorRelateAnnexSave(HttpServletRequest request) {
		BaseResponseBody<AnchorRelateAnnex> result = null;
		try {
			// 请求对象
			AnchorRelateAnnex anchorRelateAnnex = this.getParameterBean(AnchorRelateAnnex.class);

			if (StringUtils.isNotBlank(anchorRelateAnnex.getId())) {
				this.anchorRelateAnnexService.updateByPrimaryKey(anchorRelateAnnex);
			} else {

				this.anchorRelateAnnexService.insertSelective(anchorRelateAnnex);
			}
			result = new BaseResponseBody<AnchorRelateAnnex>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), anchorRelateAnnex);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<AnchorRelateAnnex>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 编辑主播信息（才艺视频、才艺图片、主播标记（逗号分割））
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorRelateAnnexUpdate.json")
	@RequiredParams(names = {})
	public BaseResponseBody anchorRelateAnnexUpdate(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			List<AnchorRelateAnnex> anchorRelateAnnexList = this.getParameterList("anchorRelateAnnexList", AnchorRelateAnnex.class);
			List<AnchorRelateGroup> anchorRelateGroupList = this.getParameterList("anchorRelateGroupList", AnchorRelateGroup.class);// 主播关联分组信息
			Anchor anchor = this.getParameterBean(Anchor.class);
			AnchorRelateAnnex relateAnnex = new AnchorRelateAnnex();
			if (anchor.getAnchorId() != null && anchor.getAnchorSign() != null && anchor.getAnchorId() != null) {
				// 标记
				Anchor sign = this.anchorService.selectByAnchorId(anchor.getAnchorId());
				anchor.setId(sign.getId());
				this.anchorService.updateByPrimaryKeySelective(anchor);
			}

			if (anchorRelateAnnexList != null && anchorRelateAnnexList.size() > 0) {
				// 库数据
				relateAnnex.setAnchorId(anchor.getAnchorId());
				List<AnchorRelateAnnex> anchorRelateAnnexes = this.anchorRelateAnnexService.selectList(relateAnnex);
				if (anchorRelateAnnexes != null && anchorRelateAnnexes.size() > 0) {
					for (int i = 0; i < anchorRelateAnnexes.size(); i++) {
						boolean flag = false;
						AnchorRelateAnnex anchorRelateAnnex = anchorRelateAnnexes.get(i);
						for (int k = 0; k < anchorRelateAnnexList.size(); k++) {
							AnchorRelateAnnex relateAnnex1 = anchorRelateAnnexList.get(k);
							if (anchorRelateAnnex.getId().equals(relateAnnex1.getId())) {
								flag = true;
								break;
							}
						}
						// 没有就删除
						if (flag == false) {
							this.anchorRelateAnnexService.deleteByPrimaryKey(anchorRelateAnnex.getId());
						}
					}
				}
				// 传进来
				for (int i = 0; i < anchorRelateAnnexList.size(); i++) {
					AnchorRelateAnnex anchorRelateAnnex = anchorRelateAnnexList.get(i);
					if (anchorRelateAnnex.getType() != null) {
						this.anchorRelateAnnexService.insertSelective(anchorRelateAnnex);
					}
				}
				// 分组信息
				if (anchorRelateGroupList != null && anchorRelateGroupList.size() > 0) {
					for (AnchorRelateGroup anchorRelateGroup : anchorRelateGroupList) {
						if (anchorRelateGroup.getId() != null && StringUtils.isNotEmpty(anchorRelateGroup.getId())) {
							// 删库中数据
							this.anchorRelateGroupService.deleteByPrimaryKey(anchorRelateGroup.getId());
						}
					}

					// 新增
					for (AnchorRelateGroup anchorRelateGroup : anchorRelateGroupList) {
						this.anchorRelateGroupService.insertSelective(anchorRelateGroup);
					}
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorRelateAnnexDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> anchorRelateAnnexDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.anchorRelateAnnexService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
